package net.starwifi.duobao.web;

import net.starwifi.duobao.entity.GiftsProduct;
import net.starwifi.duobao.entity.GiftsProductLog;
import net.starwifi.duobao.entity.Member;
import net.starwifi.duobao.mvc.BaseController;
import net.starwifi.duobao.mvc.Message;
import net.starwifi.duobao.service.DBOrderService;
import net.starwifi.duobao.service.GiftsProductLogService;
import net.starwifi.duobao.service.GiftsProductService;
import net.starwifi.duobao.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 新年活动(领取流量包产品)
 * Created by ruoo on 16/1/26.
 */
@Controller("apiYearDiscountController")
@RequestMapping(value = "/yd")
public class YearDiscountController extends BaseController {

    @Autowired
    GiftsProductService giftsProductService;

    @Autowired
    GiftsProductLogService giftsProductLogService;

    @Autowired
    MemberService memberService;

    @Autowired
    DBOrderService dbOrderService;

    /**
     * 活动主页
     */
    @RequestMapping(value = "/2016", method = RequestMethod.GET)
    public  String yearDiscount(HttpServletRequest request,Model model) {
        List<GiftsProduct> giftsProductList=giftsProductService.getListData("o.isMarketable=?1",new Object[]{Boolean.TRUE});
        model.addAttribute("productList",giftsProductList);
        return "yd/yd-2016";
    }

    /**
     * 礼包，优惠券领取
     */
    @ResponseBody
    @RequestMapping(value = "/yd-receive-{id}")
    public Message yearDiscount(@PathVariable Long id, HttpServletRequest request) {
        Member member=memberService.getCurrent();
        GiftsProduct giftsProduct=giftsProductService.find(id);
        if(giftsProduct==null){
            return Message.error("对不起,该活动不存在");
        }
        if(giftsProduct.getEndDate().before(new Date())){
            return Message.error("对不起,该礼包活动已经过期");
        }
        if(giftsProduct.getType().equals("llb")){
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = null;
            try {
                date = format.parse("2016-02-03 23:59:59");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if(member.getCreateDate().before(date)){
                return Message.error("对不起,您是老用户，无法领取流量包");
            }
            Long sumAmount=dbOrderService.getQueryCount("o.memberId=?1 and paymentStatus=?2 and amount>=?3",new Object[]{member.getId(),1, BigDecimal.ONE});
            if(sumAmount<1){
                return Message.error("对不起,您以往消费额未满1元");
            }
        }
        //获取用户领取记录
        Long count=giftsProductLogService.getQueryCount("o.giftsId=?1 and o.memberId=?2", new Object[]{id, member.getId()});
        if(count>0){
            return Message.error("您已经领取过该礼包,请到夺宝个人中心（我的礼包）查看");
        }
        //领取
        GiftsProductLog giftsProductLog=new GiftsProductLog(member.getId(),member.getMobile(),id,giftsProduct.getName(),giftsProduct.getShowLogo(),Boolean.FALSE);
        giftsProductLogService.save(giftsProductLog);
        return Message.error("领取成功,请关注微信公众号进行激活");
    }




}
